<article id="wikiArticle">
<div></div>
<p><code><strong>Symbol.toPrimitive</strong></code> 指将被调用的指定函数值的属性转换为相对应的原始值。</p>
<div><table class="standard-table">
<thead>
<tr>
<th class="header" colspan="2"><code>Symbol.toPrimitive</code> 属性的属性特性：</th>
</tr>
</thead>
<tbody>
<tr>
<td>writable</td>
<td>false</td>
</tr>
<tr>
<td>enumerable</td>
<td>false</td>
</tr>
<tr>
<td>configurable</td>
<td>false</td>
</tr>
</tbody>
</table></div>
<h2 id="描述">描述</h2>
<p>在 <code>Symbol.toPrimitive</code> 属性(用作函数值)的帮助下，一个对象可被转换为原始值。<code>该函数由字符串参数 hint 调用，</code>目的是指定原始值转换结果的首选类型。 hint 参数可以是<code>"number"、</code><code>"string" </code>和 <code>"default" 中的一种。</code></p>
<h2 id="示例">示例</h2>
<p>下列的例子展示了 <code>Symbol.toPrimitive</code> 属性如何将对象转换为原始值。</p>
<pre><code  class="language-javascript">// 没有 Symbol.toPrimitive 属性的对象
var obj1 = {};
console.log(+obj1);     // NaN
console.log(`${obj1}`); // "[object Object]"
console.log(obj1 + ""); // "[object Object]"

// 拥有 Symbol.toPrimitive 属性的对象
var obj2 = {
  [Symbol.toPrimitive](hint) {
    if (hint == "number") {
      return 10;
    }
    if (hint == "string") {
      return "hello";
    }
    return true;
  }
};
console.log(+obj2);     // 10      -- hint is "number"
console.log(`${obj2}`); // "hello" -- hint is "string"
console.log(obj2 + ""); // "true"  -- hint is "default"
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">注释</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-symbol.toprimitive" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Symbol.toPrimitive</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>原始定义</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-symbol.toprimitive" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Symbol.toPrimitive</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>48</td>
<td><a href="/en-US/Firefox/Releases/44" title="Released on 2016-01-26.">44</a> (44)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>44.0 (44)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="其他资料">其他资料</h2>
<ul>
<li><a href="Reference/Global_Objects/Date/@@toPrimitive" title="[@@toPrimitive]() 方法可以转换一个 Date 对象到一个原始值。"><code>Date.prototype[@@toPrimitive]</code></a></li>
<li><a href="Reference/Global_Objects/Symbol/@@toPrimitive" title="[@@toPrimitive]() 方法可将 Symbol 对象转换为原始值。"><code>Symbol.prototype[@@toPrimitive]</code></a></li>
<li><a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>Object.prototype.toString()</code></a></li>
<li><a href="Reference/Global_Objects/Object/valueOf" title="valueOf() 方法返回指定对象的原始值。"><code>Object.prototype.valueOf()</code></a></li>
</ul>
</article>